﻿package com.oyjd.ybx.business.favorite.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.oyjd.ybx.bean.vo.UserVO;
import com.oyjd.ybx.business.favorite.ToFavoriteExVO;
import com.oyjd.ybx.business.favorite.service.FavoriteService;
import com.oyjd.ybx.fw.action.BaseAction;
import com.oyjd.ybx.fw.util.CodeUtil;
import com.oyjd.ybx.fw.util.OutUtil;

/**
 * 
 * 收藏用户Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/favorite")
@Results({})
public class FavoriteAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 45354347027015039L;

	private static final Log log = LogFactory.getLog(FavoriteAction.class);
	private FavoriteService service;

	@Autowired
	public void setService(FavoriteService service) {
		this.service = service;
	}

	/**
	 * 获取收藏用户列表数据
	 * 
	 * @return
	 */
	public String getList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			List<ToFavoriteExVO> tmp = new ArrayList<ToFavoriteExVO>();
			if (null != userVO) {
				tmp = service.getList(userVO.getAccount());
			}
			map.put("info", tmp);
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 新增收藏用户
	 * 
	 * @return
	 */
	public String add() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String goodsId = ServletRequestUtils.getStringParameter(req, "goodsId");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			if (null != userVO) {
				boolean flag = service.add(goodsId, userVO.getAccount(), req.getRemoteAddr());
				map.put("code", flag ? CodeUtil.SUCCESS : CodeUtil.MUTI);
			} else {
				map.put("code", CodeUtil.NO_LOGIN);
			}
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}
	
	/**
	 * 新增喜欢用户
	 * 
	 * @return
	 */
	public String addLike() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String goodsId = ServletRequestUtils.getStringParameter(req, "goodsId");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			if (null != userVO) {
				boolean flag = service.addLike(goodsId, userVO.getAccount(), req.getRemoteAddr());
				map.put("code", flag ? CodeUtil.SUCCESS : CodeUtil.MUTI);
			} else {
				map.put("code", CodeUtil.NO_LOGIN);
			}
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 删除收藏用户
	 * 
	 * @return
	 */
	public String remove() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String id = ServletRequestUtils.getStringParameter(req, "id");
			service.remove(id);
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}
}
